package com.wtwd.campus.hxy.service.impl;

import com.wtwd.campus.hxy.constant.RespCode;
import com.wtwd.campus.hxy.dao.DeviceMapper;
import com.wtwd.campus.hxy.entity.DeviceInfo;
import com.wtwd.campus.hxy.model.entity.HxyStudent;
import com.wtwd.campus.hxy.model.vo.LocationVO;
import com.wtwd.campus.hxy.model.vo.ResultVO;
import com.wtwd.campus.hxy.service.DeviceService;
import com.wtwd.campus.hxy.service.HxyStudentService;
import com.wtwd.campus.utils.DateUtils;
import com.wtwd.campus.utils.GPSUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zdl
 * @Description
 * @create 2021-07-26 14:40
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private HxyStudentService hxyStudentService;

    @Override
    public LocationVO getLastLocationByImei(String imei) {
        // 查询设备数据
        DeviceInfo deviceInfo = deviceMapper.getDeviceInfoByImei(imei);
        if(deviceInfo != null){
            int battery = deviceInfo.getBattery();
            LocationVO location = deviceMapper.getLastLocationByImei(imei);
            location.setBattery(battery);
            // 经纬度转换
            double[] doubles = GPSUtils.gps84_To_Gcj02(location.getLatitude(), location.getLongitude());
            location.setLatitude(doubles[0]);
            location.setLongitude(doubles[1]);
            return location;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getTrackByImeiAndTime(String imei, long beginTime, long endTime) {
        List<Map<String, Object>> locationByImeiAndTime = deviceMapper.getLocationByImeiAndTime(imei, beginTime, endTime);
        for(Map<String,Object> map : locationByImeiAndTime){
            long time = (Long)map.get("uploadTime");
            double lat = Double.valueOf((String)map.get("latitude"));
            double lgt = Double.valueOf((String)map.get("longitude"));
            double[] doubles = GPSUtils.gps84_To_Gcj02(lat, lgt);
            map.put("uploadTime", DateUtils.format(new Date(time),DateUtils.dateFormat4));
            map.put("latitude",doubles[0]);
            map.put("longitude",doubles[1]);
        }
        return locationByImeiAndTime;
    }

    @Override
    public Object bind(HxyStudent student) {
        if (StringUtils.isAnyBlank(student.getStudentId(), student.getStudentName()
                , student.getStudentNo(), student.getImei())) {
            return ResultVO.error(RespCode.PARAMS_ERROR);
        }
        // TODO 一个学生能否绑定多个设备，如果出现重复绑定怎么办？
        try{
            hxyStudentService.save(student);
        }catch (DuplicateKeyException e){
            return ResultVO.error(RespCode.FAIL);
        }
        return ResultVO.okWithOutData();
    }

    /**
     * 解除绑定,
     * 1.恢复设备出厂设置
     * 2.删除设备相关数据
     *
     * @param imei
     * @return
     */
    @Override
    public ResultVO unbind(String imei) {
        return clearAll(imei);
    }

    /**
     * 1.绑定学生,白名单,sos,亲情号,围栏,
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO clearAll(String imei) {
        if (!hxyStudentService.deleteByImei(imei)) {
            return ResultVO.error(RespCode.DEVICE_NO_EXIST);
        }
        DeviceInfo deviceInfo = deviceMapper.getDeviceInfoByImei(imei);
        if (deviceInfo == null) {
            return ResultVO.error(RespCode.DEVICE_NO_EXIST);
        }
        deviceMapper.clearAll(imei, deviceInfo.getDeviceId());
        return ResultVO.ok();
    }


}
